//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.IO;
using Tathata.Common.BLL.Entities;
using Tathata.Common.DAL.Managers;
using Tathata.Common.Utils;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Download manager
    /// </summary>
    public class DownloadManager
    {
        #region Methods
        /// <summary>
        /// Gets a download url for an admin area
        /// </summary>
        /// <param name="download">Download instance</param>
        /// <returns>Download url</returns>
        public static string GetAdminDownloadUrl(Download download)
        {
            if (download == null)
                throw new ArgumentNullException("download");
            string url = CommonHelper.GetStoreHTTPLocation(false) + "Administration/GetDownloadAdmin.ashx?DownloadID=" + download.DownloadID;
            return url;
        }

        ///// <summary>
        ///// Gets a download url for a product variant
        ///// </summary>
        ///// <param name="orderProductVariant">Order product variant instance</param>
        ///// <returns>Download url</returns>
        //public static string GetDownloadUrl(OrderProductVariant orderProductVariant)
        //{
        //    if (orderProductVariant == null)
        //        throw new ArgumentNullException("orderProductVariant");

        //    string url = string.Empty;
        //    ProductVariant productVariant = orderProductVariant.ProductVariant;
        //    if (productVariant != null && productVariant.IsDownload)
        //    {
        //        Download download = productVariant.Download;
        //        url = CommonHelper.GetStoreHTTPLocation(false) + "GetDownload.ashx?OrderProductVariantID=" + orderProductVariant.OrderProductVariantID;
        //    }
        //    return url;
        //}

        /// <summary>
        /// Gets a download
        /// </summary>
        /// <param name="DownloadID">Download identifier</param>
        /// <returns>Download</returns>
        public static Download GetByDownloadID(int DownloadID)
        {
            return DownloadDBManager.GetByDownloadID(DownloadID);
        }

        /// <summary>
        /// Deletes a download
        /// </summary>
        /// <param name="DownloadID">Download identifier</param>
        public static void DeleteDownload(int DownloadID)
        {
            DownloadDBManager.DeleteDownload(DownloadID);
        }

        /// <summary>
        /// Inserts a download
        /// </summary>
        /// <param name="DownloadBinary">The download binary</param>
        /// <param name="Extension">The extension</param>
        /// <param name="IsNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download InsertDownload(byte[] DownloadBinary, string Extension, bool IsNew)
        {
            return DownloadDBManager.InsertDownload(DownloadBinary, Extension, IsNew);
        }

        /// <summary>
        /// Updates the download
        /// </summary>
        /// <param name="DownloadID">The download identifier</param>
        /// <param name="DownloadBinary">The download binary</param>
        /// <param name="Extension">The extension</param>
        /// <param name="IsNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download UpdateDownload(int DownloadID, byte[] DownloadBinary, string Extension, bool IsNew)
        {
            return DownloadDBManager.UpdateDownload(DownloadID, DownloadBinary, Extension, IsNew);
        }

        /// <summary>
        /// Gets the download binary array
        /// </summary>
        /// <param name="fs">File stream</param>
        /// <param name="size">Download size</param>
        /// <returns>Download binary array</returns>
        public static byte[] GetDownloadBits(Stream fs, int size)
        {
            byte[] binary = new byte[size];
            fs.Read(binary, 0, size);
            return binary;
        }
        #endregion
    }
}
